package com.kdl.webconfig.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kdl.webconfig.converter.VOConvert;
import com.kdl.webconfig.vo.PageVO;
import com.kdl.webconfig.converter.PojoConvert;
import com.kdl.webconfig.entity.ButtonConfig;
import com.kdl.webconfig.entity.PicConfig;
import com.kdl.webconfig.service.IButtonConfigService;
import com.kdl.webconfig.service.IPicConfigService;
import com.kdl.webconfig.vo.config.PicConfigDetailVO;
import com.kdl.webconfig.vo.config.PicConfigQueryReq;
import com.kdl.webconfig.vo.config.PicConfigSaveReq;
import com.kdl.webconfig.vo.config.PicConfigVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * @author 王成杰
 * @description 图片配置business
 * @date 2022-09-05 下午3:15
 */
@Component
public class PicConfigBusiness {


    @Autowired
    private IPicConfigService iPicConfigService;

    @Autowired
    private IButtonConfigService iButtonConfigService;

    /**
     * 图片配置列表
     * @param picConfigReq
     * @return
     */
    public PageVO<PicConfigVO> page(PicConfigQueryReq picConfigReq){
        Page<PicConfig> picConfigPage = iPicConfigService.queryPage(picConfigReq);
        return buildPicConfigPageVO(picConfigPage);
    }


    private PageVO<PicConfigVO> buildPicConfigPageVO(Page<PicConfig> picConfigPage) {
        PageVO<PicConfigVO> picConfigVOPageVO = new PageVO<>();
        if(Objects.nonNull(picConfigPage)) {
            picConfigVOPageVO.setCurrentPage(picConfigPage.getCurrent());
            picConfigVOPageVO.setPageSize(picConfigPage.getSize());
            picConfigVOPageVO.setTotalPages(picConfigPage.getPages());
            picConfigVOPageVO.setTotalSize(picConfigPage.getTotal());
            picConfigVOPageVO.setList(VOConvert.parsePicVosByPojos(picConfigPage.getRecords()));
        }
        return picConfigVOPageVO;
    }

    /**
     * 新增或保存
     * @param picConfigSaveReq
     * @return
     */
    public Boolean saveOrUpdate(PicConfigSaveReq picConfigSaveReq) {
        PicConfig picConfig = PojoConvert.parsePicConfig(picConfigSaveReq);
        if(Objects.nonNull(picConfig)){
            iPicConfigService.saveOrUpdate(picConfig);
            if(!CollectionUtils.isEmpty(picConfigSaveReq.getButtonConfigs())){
                List<ButtonConfig> buttonConfigs = picConfigSaveReq.getButtonConfigs();
                for (ButtonConfig buttonConfig : buttonConfigs) {
                    if(Objects.isNull(buttonConfig.getPicId())){
                        buttonConfig.setPicId(picConfig.getId());
                    }
                    iButtonConfigService.saveOrUpdate(buttonConfig);
                }
            }
        }
        return true;
    }

    /**
     * 删除图片配置
     * @param id
     * @return
     */
    public Boolean deleteByPicId(Integer id) {
        if(Objects.isNull(id)){
            return false;
        }
        iPicConfigService.removeById(id);
        //删除关联的按钮配置
        iButtonConfigService.deleteByPicId(id);
        return true;
    }

    /**
     * 获取图片详情
     * @param id
     * @return
     */
    public PicConfigDetailVO detail(Integer id) {
        PicConfigDetailVO picConfigDetailVO = new PicConfigDetailVO();
        PicConfig byId = iPicConfigService.getById(id);
        if(Objects.nonNull(byId)){
            picConfigDetailVO.setId(byId.getId());
            picConfigDetailVO.setPicUrl(byId.getPicUrl());
            picConfigDetailVO.setTitle(byId.getTitle());
            picConfigDetailVO.setComment(byId.getComment());
            picConfigDetailVO.setState(byId.getState());
            picConfigDetailVO.setSort(byId.getSort());
            picConfigDetailVO.setType(byId.getType());
            picConfigDetailVO.setSize(byId.getSize());
            picConfigDetailVO.setUploadTime(byId.getCreateTime());
            List<ButtonConfig> buttonConfigs = iButtonConfigService.selectByPicId(id);
            picConfigDetailVO.setButtonConfigs(buttonConfigs);
        }
        return picConfigDetailVO;
    }
}
